// pages/my/carbonCurrency/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    period: "",
    carbonCurrency: 365,
    list: [
      {
        id: 1,
        goodName: "b站大会员",
        goodCarbonCurrency: 500,
        imagePath: "../../../image/flower.png",
        goodType: 1
      },
      {
        id: 2,
        goodName: "腾讯视频vip满减券",
        goodCarbonCurrency: 500,
        imagePath: "../../../image/flower.png",
        goodType: 1
      },{
        id: 3,
        goodName: "定制图片帆布包",
        goodCarbonCurrency: 1000,
        imagePath: "../../../image/flower.png",
        goodType: 1
      },{
        id: 4,
        goodName: "网易云音乐vip满减券",
        goodCarbonCurrency: 400,
        imagePath: "../../../image/flower.png",
        goodType: 1
      }
    ]
  },
  /**
   * 去碳币历史页面
   */
  goToHistory() {
    wx.navigateTo({
      url: "../../my/carbonCurrencyHistory/index"
    })
  },
  // 获取时间
  time_get() {
    var time = parseInt(new Date().getHours());
    if (6 < time && time < 10) {
      this.setData({
        period: "早上好"
      })
    } else if (10 <= time && time < 14) {
      this.setData({
        period: "中午好"
      })
    } else if (14 <= time && time < 18) {
      this.setData({
        period: "下午好"
      })
    } else {
      this.setData({
        period: "晚上好"
      })
    }
  },

    /**
   * 获取前四个商品
   */
  getAll() {
    var that = this
    
    // 获取前四个商品
    wx.request({
      url: app.globalData.url + '/store/',
      method: "GET",
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)

        if(res.data.status_code == 500){
          wx.showToast({
            title: '获取失败',
            duration: 2000,
            icon: 'error',
          })
        }else {
          var temp = []
          for(var i = 0; i < 4 && i < res.data.length; i++){
            temp.push(res.data[i])
          }

          that.setData({
            list: temp
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          duration: 2000,
          icon: 'error',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.time_get()

    this.setData({
      carbonCurrency: options.num
    })
    
    this.getAll()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})